﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UpdateServers.Models;

namespace UpdateServers.Command
{
    /// <summary>
    /// 动态文件帮助类
    /// </summary>
    public class FileConfigHelper
    {
        private static readonly object _locker = new object();
        /// <summary>
        /// 配制文件名
        /// </summary>
        private const string FileConfigName = "FileConfig.json";
        /// <summary>
        /// 文件路径
        /// </summary>
        private static string FileRoot;
        static FileConfigHelper()
        {
            var Root = Directory.GetCurrentDirectory();
            FileRoot = $"{Root}\\{FileConfigName}";
        }

        /// <summary>
        /// 保存配制文件
        /// </summary>
        /// <param name="fileConfigModels"></param>
        public static void SaveFile(FileConfigModels fileConfigModels)
        {
            lock (_locker)
            {
                fileConfigModels = fileConfigModels ?? new FileConfigModels();
                try
                {
                    if (!File.Exists(FileRoot))
                    {
                        File.Create(FileRoot).Close();
                    }
                    File.WriteAllText(FileRoot, fileConfigModels.ToJsonString());
                    
                }
                catch (Exception ex)
                {
                    Loger.Log(ex.Message, "Error");
                }
;
            }
        }
        /// <summary>
        /// 获取配制
        /// </summary>
        /// <returns></returns>
        public static FileConfigModels GetFileConfig()
        {

            FileConfigModels result = null;
            lock (_locker)
            {
                if (!File.Exists(FileRoot))
                {
                    File.Create(FileRoot).Close();
                }
                var json = File.ReadAllText(FileRoot, Encoding.UTF8);
                if (!string.IsNullOrWhiteSpace(json))
                {
                    result = json.JsonToObject<FileConfigModels>();
                }
            }

            return result;
        }
        /// <summary>
        /// 设置配制
        /// </summary>
        /// <param name="fileConfigModels"></param>
        /// <returns></returns>
        public static void SetFileConfig(FileConfigModels fileConfigModels)
        {

            if (fileConfigModels != null)
            {
                var rdata = GetFileConfig();
                if (rdata != null) {
                    fileConfigModels.IpAddr = fileConfigModels.IpAddr ?? rdata.IpAddr;
                    fileConfigModels.Port = fileConfigModels.Port ?? rdata.Port;
                    fileConfigModels.FolderRoot = fileConfigModels.FolderRoot ?? rdata.FolderRoot;
                    SaveFile(fileConfigModels);
                }
                else
                {
                    SaveFile(fileConfigModels);
                }
               

            }

        }

    }
}
